package com.chefu.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.chefu.common.exception.pojo.PageResult;
import com.chefu.entity.dto.OrderDto;
import com.chefu.entity.pojo.Order;
import com.chefu.entity.pojo.OrderDetail;
import com.chefu.entity.pojo.OrderLog;
import com.chefu.order.mapper.OrderDetailMapper;
import com.chefu.order.mapper.OrderLogMapper;
import com.chefu.order.mapper.OrderMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogMapper orderLogMapper;

    public Order findOrderById(Long id) {
        Order order = orderMapper.selectById(id);
        return order;
    }

    public PageResult<Order> orderPageQuery(Integer page, Integer rows, String startTime, String endTime, Order pageOrder, String type) {
        IPage<Order> iPage = new Page(page, rows);
        //QueryWrapper<Order> queryWrapper = Wrappers.query(pageOrder);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();

        String contact = pageOrder.getContact();
        String storeName = pageOrder.getStoreName();

        if (type.equals("1")) {
            queryWrapper.eq("type", type);
        }

        if (type.equals("2")) {
            queryWrapper.eq("type", type);
        }

        if (StringUtils.isNotEmpty(contact)) {
            pageOrder.setContact(null);
            queryWrapper.like("contact", contact);
        }
        if (StringUtils.isNotEmpty(storeName)) {
            pageOrder.setStoreName(null);
            queryWrapper.like("store_name", storeName);
        }

        if (startTime != null) {

            queryWrapper.eq("create_date", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("create_date", endTime);
        }

        iPage = orderMapper.selectPage(iPage, queryWrapper);
        PageResult<Order> pageResult = new PageResult<>(iPage.getTotal(), iPage.getPages(), iPage.getRecords());

        return pageResult;
    }


    public PageResult<Order> orderStatusPageQuery(Integer page, Integer rows, Date startTime, Date endTime, Order pageOrder,String Status) {
        IPage<Order> iPage = new Page(page, rows);
        QueryWrapper<Order> queryWrapper = Wrappers.query(pageOrder);

        String contact = pageOrder.getContact();
        String storeName = pageOrder.getStoreName();

        if (Status.equals("1")) {
            queryWrapper.eq("order_status", Status);
        }

        if (StringUtils.isNotEmpty(contact)) {
            pageOrder.setContact(null);
            queryWrapper.like("contact", contact);
        }
        if (StringUtils.isNotEmpty(storeName)) {
            pageOrder.setStoreName(null);
            queryWrapper.like("store_name", storeName);
        }
        if (startTime != null) {

            queryWrapper.eq("create_date", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("create_date", endTime);
        }

        iPage = orderMapper.selectPage(iPage, queryWrapper);
        PageResult<Order> pageResult = new PageResult<>(iPage.getTotal(), iPage.getPages(), iPage.getRecords());

        return pageResult;
    }

    public OrderDto finorderDetailByid(Long id) {
        OrderDto orderDto = new OrderDto();
        Order order = orderMapper.selectById(id);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        QueryWrapper<OrderDetail> queryWrapper = Wrappers.query(orderDetail);
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);


        OrderLog orderLog = new OrderLog();
        orderLog.setOrderId(order.getId());
        QueryWrapper<OrderLog> query = Wrappers.query(orderLog);
        List<OrderLog> orderLogs = orderLogMapper.selectList(query);

        orderDto.setOrder(order);
        orderDto.setOrderDetails(orderDetails);
        orderDto.setOrderLogs(orderLogs);
        return orderDto;
    }
}
